/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.service.impl;

import com.yanlight.app.dal.dao.CompanyAdTypeDAO;
import com.yanlight.app.dal.dao.CompanyDAO;
import com.yanlight.app.dal.entity.Company;
import com.yanlight.app.dal.entity.CompanyAdType;
import com.yanlight.app.response.CompanyNameResponse;
import com.yanlight.app.service.CompanyService;
import com.yanlight.type.ReviewType;
import com.yanlight.type.StatusType;
import com.yanlight.util.DateTimeUtil;
import com.yanlight.util.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author haolw
 * @time 2017/12/07
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    private final String COMPANY_KEY_PREFIX = "trade_company_";
    @Autowired
    private CompanyDAO companyDAO;

    @Autowired
    private CompanyAdTypeDAO companyAdTypeDAO;

    @Override
    public Company findById(Integer companyId) {
        Company company = null;
        String key = getCompanyKey(companyId);
        Boolean exists = JedisUtil.exists(key);

        if (!exists) {
            company = companyDAO.findById(companyId);
            if (company != null) {
                CompanyAdType adType = companyAdTypeDAO.findAdType(companyId);
                if (adType != null) {
                    company.setAdType(adType.getAdTypeId());
                }

                JedisUtil.set(key, company);
            }
        } else {
            company = JedisUtil.get(key, Company.class);
        }
        return company;
    }

    @Override
    public boolean checkAvailable(Integer companyId) {
        if (companyId == null) {
            return false;
        }
        Company company = findById(companyId);
        if (company == null) {
            return false;
        }
        Integer status = company.getStatus();
        if (status.equals(StatusType.UNACTIVITY.ordinal())) {
            return false;
        }
        Integer reviewType = company.getReviewType();
        if (reviewType.equals(ReviewType.REVIEWING.ordinal())) {
            return false;
        }
        String expireTime = company.getExpireTime();
        if (DateTimeUtil.checkExpire(expireTime)) {
            return false;
        }
        return true;
    }

    @Override
    public CompanyNameResponse findName(Integer companyId) {
        CompanyNameResponse response = null;
        Company company = findById(companyId);
        if (company != null) {
            response = new CompanyNameResponse();
            response.setName(company.getName());
            response.setShortName(company.getShortName());
            response.setCompanyId(company.getId().toString());
            response.setLogo(company.getLogo());
            response.setAlias(company.getAlias());
            response.setAdType(company.getAdType());
        }
        return response;
    }

    private String getCompanyKey(Integer companyId) {
        return COMPANY_KEY_PREFIX + companyId;
    }

}
